package autoteam.ui.tabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.TableModelEvent;

import autoteam.League;
import autoteam.tables.DraftTypeTableModel;
import autoteam.ui.AutoTeamTablePanel;

public class DraftTypeTab extends TabPanel
{
    private static final long serialVersionUID = -4238233865724303188L;
    
    public DraftTypeTab( final League currentLeague )
    {
	super( );
	// The work area
	final DraftTypeTableModel model = new DraftTypeTableModel( null, currentLeague );
	final AutoTeamTablePanel theTablePanel = new AutoTeamTablePanel( model );
	
	// The specialized cell editor
	//
	DefaultComboBoxModel cbmodel = new DefaultComboBoxModel( );
	for ( int i = 0; i < currentLeague.getNumberOfSkillFields( ); i++ )
	    cbmodel.insertElementAt( currentLeague.getSkillName( i ), i );
	final JComboBox comboBox = new JComboBox( cbmodel );
	theTablePanel.setTableCellEditor( 0, comboBox );
	if ( model.getColumnCount( ) > 3 )
	    theTablePanel.setTableCellEditor( 3, comboBox );
	
	// The radio buttons
	//
	final JRadioButton hRadioButton = new JRadioButton( "Simple" );
	final JRadioButton pRadioButton = new JRadioButton( "Positional" );
	
	// Group the radio buttons.
	ButtonGroup group = new ButtonGroup( );
	group.add( hRadioButton );
	group.add( pRadioButton );
	
	// Register a listener for the radio buttons.
	hRadioButton.addActionListener( new ActionListener( )
	{
	    public void actionPerformed( ActionEvent e )
	    {
		theTablePanel.stopEditing( );
		currentLeague.setDraftType( 0 );
		model.hierarchical( );
		model.fireTableChanged( new TableModelEvent( model, TableModelEvent.HEADER_ROW ) );
		theTablePanel.setTableCellEditor( 0, comboBox );
	    }
	} );
	pRadioButton.addActionListener( new ActionListener( )
	{
	    public void actionPerformed( ActionEvent e )
	    {
		theTablePanel.stopEditing( );
		currentLeague.setDraftType( 1 );
		model.positional( );
		model.fireTableChanged( new TableModelEvent( model, TableModelEvent.HEADER_ROW ) );
		theTablePanel.setTableCellEditor( 0, comboBox );
		theTablePanel.setTableCellEditor( 3, comboBox );
		
	    }
	} );
	
	if ( currentLeague.getDraftType( ) == 0 )
	    hRadioButton.setSelected( true );
	else if ( currentLeague.getDraftType( ) == 1 )
	    pRadioButton.setSelected( true );
	
	JPanel buttonPanel = new JPanel( );
	buttonPanel.add( hRadioButton );
	buttonPanel.add( pRadioButton );
	
	theTablePanel.addComponent( buttonPanel );
	
	JPanel panel = new JPanel( );
	panel.add( theTablePanel );
	this.add( panel );
    }

	@Override
	protected void setButtonActions(JFrame parentFrame)
	{
		// TODO Auto-generated method stub
		
	}
}
